package com.campus.client.controller;

import com.campus.client.net.ClientConnector;
import com.campus.common.message.BankResponse;
import com.campus.common.message.BankRequest;
import com.campus.common.vo.BankTransaction;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BankController {
    private ClientConnector connector;

    public BankController(ClientConnector connector) {
        this.connector = connector;
    }

    // 查询余额
    public double queryBalance(String userId) throws IOException, ClassNotFoundException {
        BankResponse resp = connector.queryBalance(userId);
        return resp.isSuccess() ? resp.getBalance() : -1;
    }

    // 支付（消费）
    public boolean pay(String userId, double amount) throws IOException, ClassNotFoundException {
        BankResponse resp = connector.pay(userId, amount);
        return resp.isSuccess();
    }

    // 充值
    public boolean recharge(String userId, double amount, String password) throws IOException, ClassNotFoundException {
        BankResponse resp = connector.recharge(userId, amount, password);
        return resp.isSuccess();
    }

    // 开户
    public boolean openAccount(String userId, double initBalance, String password) throws IOException, ClassNotFoundException {
        BankResponse resp = connector.openAccount(userId, initBalance, password);
        return resp.isSuccess();
    }
    
    // 销户
    public boolean deleteAccount(String userId, String password) throws IOException, ClassNotFoundException {
        BankResponse resp = connector.deleteAccount(userId, password);
        return resp.isSuccess();
    }
    
    // 查询流水
    public List<BankTransaction> queryTransactions(String userId) throws IOException, ClassNotFoundException {
        BankResponse resp = connector.queryTransactions(userId);
        return resp.isSuccess() ? resp.getTransactions() : new ArrayList<>();
    }
    

    // 可选：结束会话
    public void closeConnection() throws IOException {
        connector.close();
    }
}
